package oldCodePackage;
import java.io.IOException;


public abstract class Player {
	public static int trumpSuit = -1;
	public static int startSuit = -1;
	public static int highestBid = -1;
	public static String highestBidder = "Noone";
	protected int numCards = 0;
	protected int score = 0;
	protected int tricksBid = -1;
	protected int tricksTaken = 0;
	protected Card myHand[];
	protected Boolean myHandPlayed[];
	protected String myName;
	protected int numPlayable = 0;
	protected Boolean[] playableCard;
		
	//plays a card from hand
	abstract public Card playCard();
	
	//places a bid at the start of a round
	abstract public int placeBid() throws IOException;
	
	//clears old hands and deals a new hand
	public void receiveHand(Card hand[]){
		myHand = null;
		myHandPlayed = null;
		
		numCards = hand.length;
		myHand = new Card[numCards];
		myHandPlayed = new Boolean[numCards];
		for(int i=0; i<numCards; i++){
			myHand[i] = hand[i];
			myHandPlayed[i] = false;
		}
	}

	public void addScore(int score) {
		this.score += score;
	}

	public int getScore() {
		return score;
	}

	/**
	 * @return the tricksBid
	 */
	public int getTricksBid() {
		return tricksBid;
	}

	/**
	 * 
	 */
	public void addTricksTaken() {
		this.tricksTaken++;
	}

	/**
	 * @return the tricksTaken
	 */
	public int getTricksTaken() {
		return tricksTaken;
	}
	
	//sets cards as playable for a given hand
	protected void getPlayableCards(){
		playableCard = null;
		playableCard = new Boolean[numCards];
		int cardsOfStartSuit = 0;
		
		if(startSuit == -1 || trumpSuit == Game.ALL_TRUMP) //first to play or all trump so all cards are playable
			for(int i=0; i<numCards; i++)
				playableCard[i] = true;
		else if(startSuit != Game.NO_TRUMP){ //find starting suit if any for no trump
			for(int i=0; i<numCards; i++){
				//start and trump are hearts so AoH is accounted for
				if(startSuit == Game.HEARTS && trumpSuit == Game.HEARTS){
					if(myHand[i].getSuit() == startSuit){
						playableCard[i] = true;
						cardsOfStartSuit++;
					}
				}
				//start is hearts but its not trump, remove AoH
				else if(startSuit == Game.HEARTS && trumpSuit != Game.HEARTS){
					if(myHand[i].getSuit() == startSuit && !myHand[i].equals(Game.ACE_OF_HEARTS)){
						playableCard[i] = true;
						cardsOfStartSuit++;
					}
				}
				//start is not hearts but trump are hearts so AoH is accounted for in trump
				else if(startSuit != Game.HEARTS && trumpSuit == Game.HEARTS){
					if(myHand[i].getSuit() == startSuit){
						playableCard[i] = true;
						cardsOfStartSuit++;
					}
					if(myHand[i].getSuit() == trumpSuit)
						playableCard[i] = true;
				}
				//start is not hearts nor is trump, add AoH as trump
				else if(startSuit != Game.HEARTS && trumpSuit != Game.HEARTS){
					if(myHand[i].getSuit() == startSuit){
						playableCard[i] = true;
						cardsOfStartSuit++;
					}
					if(myHand[i].getSuit() == trumpSuit || myHand[i].equals(Game.ACE_OF_HEARTS))
						playableCard[i] = true;
				}
			}
			
			if(cardsOfStartSuit == 0)//can play any card in this case, since no cards to follow suit
				for(int i=0; i<numCards; i++)
					playableCard[i] = true;
		}
		else{ // no trump case
			for(int i=0; i<numCards; i++){
				if(myHand[i].equals(Game.ACE_OF_HEARTS))//AoH is always playable
					playableCard[i] = true;
				
				if(startSuit == Game.HEARTS){ // start is hearts, dont inclue AoH
					if(myHand[i].getSuit() == startSuit && !myHand[i].equals(Game.ACE_OF_HEARTS)){
						playableCard[i] = true;
						cardsOfStartSuit++;
					}
				}
				else if(startSuit != Game.HEARTS){ // start isnt hearts, doesn't matter
					if(myHand[i].getSuit() == startSuit){
						playableCard[i] = true;
						cardsOfStartSuit++;
					}
				}
			}
			
			if(cardsOfStartSuit == 0)//can play any card in this case, since no cards to follow suit
				for(int i=0; i<numCards; i++)
					playableCard[i] = true;
		}
		
		
		for(int i=0; i<numCards; i++)//count number of playable cards
			if(playableCard[i])
				numPlayable++;
	}

	public static void resetCards() {
		startSuit = -1;
		trumpSuit = -1;
		highestBid = -1;
		highestBidder = "Noone";
	}
	
	
}
